package com.jhclothing.web.controller.product;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jhclothing.common.enums.ClothingSize;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.service.IClothingTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.product.domain.ClothingInventory;
import com.jhclothing.product.service.IClothingInventoryService;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.common.core.page.TableDataInfo;

/**
 * 服饰库存Controller
 * 
 * @author zyp
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/product/clothingInventory")
public class ClothingInventoryController extends BaseController
{
    @Autowired
    private IClothingInventoryService clothingInventoryService;

    @Autowired
    private IClothingTypeService clothingTypeService;

    /**
     * 查询服饰库存列表
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClothingInventory clothingInventory)
    {
        startPage();
        List<ClothingInventory> list = clothingInventoryService.selectClothingInventoryList(clothingInventory);
        return getDataTable(list);
    }

    /**
     * 导出服饰库存列表
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:export')")
    @Log(title = "服饰库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClothingInventory clothingInventory)
    {
        List<ClothingInventory> list = clothingInventoryService.selectClothingInventoryList(clothingInventory);
        ExcelUtil<ClothingInventory> util = new ExcelUtil<ClothingInventory>(ClothingInventory.class);
        util.exportExcel(response, list, "服饰库存数据");
    }

    /**
     * 获取服饰库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:query')")
    @GetMapping(value = {"/", "/{clothingInventoryId}"})
    public AjaxResult getInfo(@PathVariable(value = "clothingInventoryId", required = false) Long clothingInventoryId)
    {
        AjaxResult ajax = AjaxResult.success();
        List<ClothingType> clothingTypes = clothingTypeService.selectClothingTypeAll();
        ajax.put("clothingTypes", clothingTypes);
        if (StringUtils.isNotNull(clothingInventoryId)) {
            ClothingInventory clothingInventory = clothingInventoryService.selectClothingInventoryByClothingInventoryId(clothingInventoryId);
            ajax.put(AjaxResult.DATA_TAG, clothingInventory);
            ajax.put("clothingTypeId", clothingInventory.getClothingTypeId());
        }
        return ajax;
    }

    /**
     * 新增服饰库存
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:add')")
    @Log(title = "服饰库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothingInventory clothingInventory)
    {
        if (!clothingInventoryService.checkClothingTypeAndSizeUnique(clothingInventory)) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingInventory.getClothingTypeId());
            String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingInventory.getClothingSize());
            return error(String.format("服饰品类为%s，服饰尺码为%s的服饰已存在",
                    clothingType.getClothingTypeCodeAndSex(), clothingSize));
        }
        clothingInventory.setCreateBy(getUsername());
        return toAjax(clothingInventoryService.insertClothingInventory(clothingInventory));
    }

    /**
     * 修改服饰库存
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:edit')")
    @Log(title = "服饰库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothingInventory clothingInventory)
    {
        if (!clothingInventoryService.checkClothingTypeAndSizeUnique(clothingInventory)) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingInventory.getClothingTypeId());
            String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingInventory.getClothingSize());
            return error(String.format("服饰品类为%s，服饰尺码为%s的已存在",
                    clothingType.getClothingTypeCodeAndSex(), clothingSize));
        }
        clothingInventory.setUpdateBy(getUsername());
        return toAjax(clothingInventoryService.updateClothingInventory(clothingInventory));
    }

    /**
     * 删除服饰库存
     */
    @PreAuthorize("@ss.hasPermi('product:clothingInventory:remove')")
    @Log(title = "服饰库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clothingInventoryIds}")
    public AjaxResult remove(@PathVariable Long[] clothingInventoryIds)
    {
        return toAjax(clothingInventoryService.deleteClothingInventoryByClothingInventoryIds(clothingInventoryIds));
    }
}
